﻿using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class ActionUI : UIBase
{
    Chess chess;
    public Vector3 posAdjust;
    public override int UILayer => base.UILayer;
    public void Awake()
    {
        InitButtons();
    }

    public override void OnDisplay(object args)
    {
        UIManager.Instance.Open("MaskUI");
        base.OnDisplay(args);
        if(args is Chess)
        {
             chess = args as Chess;
        }
        var buttonList = this.transform.GetComponentsInChildren<Button>(true);
        if (chess.state == ChessState.Flying)
        {
            foreach (Button button in buttonList)
            {
                switch (button.name)
                {
                    case "Form":
                    case "LayMine":
                    case "Move":
                        button.gameObject.SetActive(true);
                        break;
                    case "DefuseMine":
                        button.gameObject.SetActive(false);
                        break;
                }
            }
        }
        else
        {
            foreach (Button button in buttonList)
            {
                switch (button.name)
                {
                    case "Move":
                    case "LayMine":
                        button.gameObject.SetActive(false);
                        break;
                    case "Form":
                    case "DefuseMine":
                        button.gameObject.SetActive(true);
                        break;
                }
            }
        }
        this.transform.position = chess.transform.position+ posAdjust;

    }
    private void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            UIManager.Instance.Close(this);
        }
        if (chess.Pos.y>8)
            this.transform.position = chess.transform.position + posAdjust;
        else
            this.transform.position = chess.transform.position - posAdjust;
    }

    protected override void OnClose()
    {
        base.OnClose();
        UIManager.Instance.Close("MaskUI");
    }
    void InitButtons()
    {
        Debug.Log("InitBtn");
        var buttonList = this.transform.GetComponentsInChildren<Button>(true);
        foreach (Button button in buttonList)
        {
            switch (button.name)
            {
                case "Form":
                    button.onClick.AddListener(OnFormClick);
                    break;
                case "LayMine":
                    button.onClick.AddListener(OnMineClick);
                    break;
                case "Move":
                    button.onClick.AddListener(OnMoveClick);
                    break;
                case "DefuseMine":
                    button.onClick.AddListener(OnDefuseClick);
                    break;
            }
        }
    }
    void OnFormClick()
    {
        AudioManager.Instance.PlayEffectAudio("Ensure", 0.5f);
        chess.player.ClearHighLight();
        if (chess.state == ChessState.Watching)
        {
            chess.ChangeState();
            if (chess.player.Id == 1)
            {
                chess.GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Sprites/RChess");
                TurnController.Instance.player1.EndAction();
            }
            else
            {
                chess.GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Sprites/BChess");
                TurnController.Instance.player2.EndAction();
            }
        }
        else
        {
            chess.ChangeState();
            if (chess.player.Id == 1)
            {
                chess.GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Sprites/RWChess");
                TurnController.Instance.player1.EndAction();
            }
            else
            {
                chess.GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Sprites/BWChess");
                TurnController.Instance.player2.EndAction();
            }
        }
        UIManager.Instance.Close(this);
    }
    void OnMineClick()
    {
        AudioManager.Instance.PlayEffectAudio("Ensure", 0.5f);
        chess.player.ClearHighLight();
        chess.player.MineCount = 0;
        for(int i = chess.Pos.y - 2; i <= chess.Pos.y + 2; i++)
        {
            if (i < 0 || i > 17) continue;
            for (int j = chess.Pos.x - 2; j <= chess.Pos.x + 2; j++)
            {
                if (j < 0 || j > 11) continue;
                Debug.Log("In MineClick");
                Vector2Int vector = new Vector2Int(j, i);
                if (Map.Instance.cellDIC[vector].cellState == CellState.Chess) continue;
                chess.player.HighLightCells.Add(vector);
                Map.Instance.HightLightCell(vector);
            }
        }
        chess.player.playerState = PlayerState.Mining;
        UIManager.Instance.Close(this);
    }
    void OnMoveClick()
    {
        AudioManager.Instance.PlayEffectAudio("Ensure", 0.5f);
        chess.player.ClearHighLight();
        UIManager.Instance.Close(this);
        chess.player.playerState = PlayerState.Moving;
        Debug.Log("OnMoveClick");
        chess.FindPath();
    }
    void OnDefuseClick()
    {
        AudioManager.Instance.PlayEffectAudio("Ensure", 0.5f);
        chess.player.ClearHighLight();
        chess.player.playerState = PlayerState.Defusing; 
        for (int i = chess.Pos.y - 2; i <= chess.Pos.y + 2; i++)
        {
            if (i < 0 || i > 17) continue;
            for (int j = chess.Pos.x - 2; j <= chess.Pos.x + 2; j++)
            {
                if (j < 0 || j > 11) continue;
                Vector2Int vector = new Vector2Int(j, i);
                if (Map.Instance.cellDIC[vector].cellState == CellState.Chess) continue;
                chess.player.HighLightCells.Add(vector);
                Map.Instance.HightLightCell(vector);
            }
        }

        UIManager.Instance.Close(this);
    }

}
